<template>
  <div class="app-container">

    <div class="main-container">
      <!-- 侧边栏菜单 -->
      <el-aside width="220px" class="sidebar">
        <el-menu
            :default-active="activeMenu"
            class="side-menu"
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#409EFF"
            :collapse="isCollapse"
            router>
          <template v-for="item in menuList">
            <!-- 一级菜单 -->
            <el-submenu
                v-if="item.children && item.children.length > 0"
                :key="item.path"
                :index="item.path">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                  v-for="child in item.children"
                  :key="child.path"
                  :index="child.path">
                <i :class="child.icon"></i>
                <span>{{ child.title }}</span>
              </el-menu-item>
            </el-submenu>

            <!-- 没有子菜单的一级菜单 -->
            <el-menu-item
                v-else
                :key="item.path"
                :index="item.path">
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="content">
        <router-view></router-view>
      </el-main>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MainLayout',
  data() {
    return {
      isCollapse: false,
      activeMenu: '/dashboard',
      menuList: [
        {
          path: '/dashboard',
          title: '控制台',
          icon: 'el-icon-s-home'
        },
        {
          path: '/system',
          title: '系统管理',
          icon: 'el-icon-s-tools',
          children: [
            {
              path: '/system/user',
              title: '用户管理',
              icon: 'el-icon-user'
            },
            {
              path: '/system/role',
              title: '角色管理',
              icon: 'el-icon-s-custom'
            },
            {
              path: '/system/menu',
              title: '菜单管理',
              icon: 'el-icon-menu'
            }
          ]
        },
        {
          path: '/product',
          title: '产品管理',
          icon: 'el-icon-s-goods',
          children: [
            {
              path: '/product/list',
              title: '产品列表',
              icon: 'el-icon-tickets'
            },
            {
              path: '/product/category',
              title: '产品分类',
              icon: 'el-icon-collection'
            }
          ]
        },
        {
          path: '/order',
          title: '订单管理',
          icon: 'el-icon-s-order',
          children: [
            {
              path: '/order/list',
              title: '订单列表',
              icon: 'el-icon-document'
            },
            {
              path: '/order/statistics',
              title: '订单统计',
              icon: 'el-icon-data-line'
            }
          ]
        }
      ]
    }
  },
  created() {
    // 设置当前激活的菜单
    this.activeMenu = this.$route.path
  },
  watch: {
    $route(to) {
      this.activeMenu = to.path
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;

  .header {
    background-color: #545c64;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;

    .logo {
      font-size: 20px;
      font-weight: bold;
    }

    .top-menu {
      border-bottom: none;
    }
  }

  .main-container {
    display: flex;
    flex: 1;
    overflow: hidden;

    .sidebar {
      background-color: #304156;
      height: 100%;

      .side-menu {
        height: 100%;
        border-right: none;

        &:not(.el-menu--collapse) {
          width: 220px;
        }
      }
    }

    .content {
      padding: 20px;
      background-color: #f0f2f5;
      overflow-y: auto;
    }
  }
}

// 菜单项悬停效果
.el-menu-item:hover,
.el-submenu__title:hover {
  background-color: #263445 !important;
}

// 子菜单项样式
.el-menu--inline {
  background-color: #1f2d3d !important;

  .el-menu-item {
    padding-left: 50px !important;
  }
}
</style>